package com.sgcc.fj.pmos.e_fj_home.presenter;

import android.text.TextUtils;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.sgcc.fj.pmos.common.base.BasePresenter;
import com.sgcc.fj.pmos.common.http.HttpInterConst;
import com.sgcc.fj.pmos.common.http.HttpRequestUtils;
import com.sgcc.fj.pmos.common.http.MvpNetCallback;
import com.sgcc.fj.pmos.common.utils.AssetsUtil;
import com.sgcc.fj.pmos.common.utils.BigDecimalUtils;
import com.sgcc.fj.pmos.common.utils.ListPageUtil;
import com.sgcc.fj.pmos.e_fj_home.bean.MarketBean;
import com.sgcc.fj.pmos.e_fj_home.bean.MembersMarketBean;
import com.sgcc.fj.pmos.e_fj_home.contract.SellElectricityCompanyContract;
import com.sgcc.fj.pmos.e_fj_mall.bean.MallGoodsBean;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;


public class SellElectricityCompanyPresenter extends BasePresenter<SellElectricityCompanyContract.View> implements SellElectricityCompanyContract.Presenter {


    @Override
    public void getSellElectricityCompanyList(String membersName, String credit, String num, String ele, int listSize, boolean isloading) {
        Map<String, Object> params = new HashMap<>();
        params.putAll(ListPageUtil.getPageInfo(listSize));
        params.put("membersName", membersName);
        params.put("nameSort", "");
        params.put("creditSort", credit);
        params.put("numSort", num);
        params.put("energySort", ele);
        params.put("_t", System.currentTimeMillis());
        HttpRequestUtils.getInstance().request(params, HttpInterConst.HOME_MARKETLIST, new MvpNetCallback<MarketBean>(getView(), isloading) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                if (!isViewAttached()) {
                    return;
                }
                // 失败回调 判断界面是否销毁
//                getView().queryGoodsListFailed();
            }

            @Override
            public void onResponse(MarketBean response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                if (response != null) {
                    if (response.getCode() == 200) {
                        getView().querySuccess(response.getData().isHasNextPage(), response.getData().getList());
                    } else {
                        ToastUtils.showShort(response.getMsg());
                    }
                    if (null != response.getData()) {
                        getView().totalQuerySuccess(response.getData().getTotal());
                    }
                } else {
                    ToastUtils.showShort("售电公司名单获取失败");
                }

            }
        });


//        String json = AssetsUtil.readStringFromAssets("membersmarket.json");
//                MembersMarketBean membersMarketBean = GsonUtils.fromJson(json, MembersMarketBean.class);
//        int pageNum = new ListPageUtil(listSize, ListPageUtil.pageSize).getTotalPage();
//        List<MembersMarketBean.PowerCompanyTableBean> list = null;
//        if (TextUtils.isEmpty(num) && TextUtils.isEmpty(ele)) {
//            list = membersMarketBean.getPowerCompanyTableList(pageNum, membersMarketBean.getPowerCompanyTable());
//        } else if (!TextUtils.isEmpty(num) && "1".equals(num)) {
//            list = membersMarketBean.getPowerCompanyTableList(pageNum, SortNum(membersMarketBean.getPowerCompanyTable(), false));
//        } else if (!TextUtils.isEmpty(num) && "2".equals(num)) {
//            list = membersMarketBean.getPowerCompanyTableList(pageNum, SortNum(membersMarketBean.getPowerCompanyTable(), true));
//        } else if (!TextUtils.isEmpty(ele) && "1".equals(ele)) {
//            list = membersMarketBean.getPowerCompanyTableList(pageNum, SortEle(membersMarketBean.getPowerCompanyTable(), false));
//        } else if (!TextUtils.isEmpty(ele) && "2".equals(ele)) {
//            list = membersMarketBean.getPowerCompanyTableList(pageNum, SortEle(membersMarketBean.getPowerCompanyTable(), true));
//        } else {
//            list = membersMarketBean.getPowerCompanyTableList(pageNum, membersMarketBean.getPowerCompanyTable());
//        }
//        boolean isHasNextPage = ListPageUtil.isHasNextPage(membersMarketBean.getPowerCompanyTable().size(), pageNum);
//        getView().querySuccess(isHasNextPage, list);
    }

    /**
     * @param List 要排序的数组
     * @param type 是否要降序排
     */
    public List<MembersMarketBean.PowerCompanyTableBean> SortNum(List<MembersMarketBean.PowerCompanyTableBean> List, boolean type) {
        //排序
        Collections.sort(List, new Comparator<MembersMarketBean.PowerCompanyTableBean>() {
            @Override
            public int compare(MembersMarketBean.PowerCompanyTableBean o1, MembersMarketBean.PowerCompanyTableBean o2) {
                // 进行排序
                if (BigDecimalUtils.compareTo(new BigDecimal(o1.getNum()), new BigDecimal(o2.getNum())) == 1) {//>01>02
                    return 1;
                }
                if (BigDecimalUtils.compareTo(new BigDecimal(o1.getNum()), new BigDecimal(o2.getNum())) == 0) {//01=02
                    return 0;
                }
                return -1;
            }
        });
        //降序
        if (type) {
            Collections.reverse(List);
        }
        return List;
    }

    /**
     * @param List 要排序的数组
     * @param type 是否要降序排
     */
    public List<MembersMarketBean.PowerCompanyTableBean> SortEle(List<MembersMarketBean.PowerCompanyTableBean> List, boolean type) {
        //排序
        Collections.sort(List, new Comparator<MembersMarketBean.PowerCompanyTableBean>() {
            @Override
            public int compare(MembersMarketBean.PowerCompanyTableBean o1, MembersMarketBean.PowerCompanyTableBean o2) {
                // 进行排序
                if (BigDecimalUtils.compareTo(new BigDecimal(o1.getMonthAvgPower()), new BigDecimal(o2.getMonthAvgPower())) == 1) {//>01>02
                    return 1;
                }
                if (BigDecimalUtils.compareTo(new BigDecimal(o1.getMonthAvgPower()), new BigDecimal(o2.getMonthAvgPower())) == 0) {//01=02
                    return 0;
                }
                return -1;
            }
        });
        //降序
        if (type) {
            Collections.reverse(List);
        }
        return List;
    }

}
